home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 24
/
Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso
/
Aminet
/
dev
/
c
/
cpost_1_4.lha
/
parsearg.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-09
|
14KB
|
392 lines
/*------------------------------------------------------------------*/
/* parsearg : parse parameters and options in an argv list */
/* see parsearg.h for a description */
/*------------------------------------------------------------------*/
/* 03-13-90 originally by Patrick J. Mueller */
/* 01-09-91 version 2.0 by Patrick J. Mueller */
/* 04-29-91 version 3.0 by Patrick J. Mueller */
/*------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
/*------------------------------------------------------------------*/
/* typedefs */
/*------------------------------------------------------------------*/
typedef enum
{
Boolean_Switch,
Variable_Switch
} Item_Type;
typedef struct Cmdline_Item
{
Item_Type type;
int position;
char sw_char;
void *variable;
struct Cmdline_Item *next;
} Cmdline_Item;
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*------------------------------------------------------------------*/
/* L O C A L F U N C T I O N S */
/*------------------------------------------------------------------*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------*/
/* Search the Cmdline_Item list for a particular switch. Look for */
/* the option with a particular switch character (Boolean and */
/* Variable are treated as the same) */
/*------------------------------------------------------------------*/
static Cmdline_Item *get_item(
Cmdline_Item *head,
char sw_char,
int case_sense
)
{
Cmdline_Item *next;
/*---------------------------------------------------------------*/
/* traverse the linked list ... */
/*---------------------------------------------------------------*/
next = head;
while (next != NULL)
{
/*------------------------------------------------------------*/
/* for case sensitive switches, just compare chars */
/*------------------------------------------------------------*/
if (case_sense)
{
if (next->sw_char == sw_char)
return(next);
}
/*------------------------------------------------------------*/
/* otherwise, toupper the chars and compare */
/*------------------------------------------------------------*/
else
{
if (toupper(sw_char) == toupper(next->sw_char))
return(next);
}
/*------------------------------------------------------------*/
/* no matches so traverse to next item */
/*------------------------------------------------------------*/
next = next->next;
}
/*---------------------------------------------------------------*/
/* no matches at all! */
/*---------------------------------------------------------------*/
return(NULL);
}
/*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
/*------------------------------------------------------------------*/
/* M A I N F U N C T I O N */
/*------------------------------------------------------------------*/
/*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
/*------------------------------------------------------------------*/
/* the main function */
/*------------------------------------------------------------------*/
void parsearg(
int *argc,
char **argv,
int case_sense,
char *env_var,
char *delimiters,
char *format_string,
...
)
{
char *tok;
Cmdline_Item *item_head;
Cmdline_Item *item_tail;
Cmdline_Item *item;
va_list arg_marker;
int i;
int parms;
int *ptr_int;
char **ptr_ptr_char;
char sw_char;
char *env_value;
int envc;
char *envv;
char *temp;
/*---------------------------------------------------------------*/
/* sanity checks */
/*---------------------------------------------------------------*/
if ((NULL == format_string) ||
(NULL == argv) ||
(0 == *argc))
return;
/*---------------------------------------------------------------*/
/* make a copy of the format string since we will be strtok()ing */
/* through it */
/*---------------------------------------------------------------*/
temp = malloc(1+strlen(format_string));
if (NULL == temp)
{
puts("Error allocating memory in parsearg()");
return;
}
strcpy(temp,format_string);
format_string = temp;
/*---------------------------------------------------------------*/
/* get environment variable value */
/*---------------------------------------------------------------*/
env_value = NULL;
if (NULL != env_var)
if ('\0' != *env_var)
{
/*---------------------------------------------------------*/
/* get value and copy if we found something */
/*---------------------------------------------------------*/
env_value = getenv(env_var);
if (NULL != env_value)
{
temp = malloc(1+strlen(env_value));
if (NULL == temp)
{
puts("Error allocating memory in parsearg()");
return;
}
strcpy(temp,env_value);
env_value = temp;
}
}
/*---------------------------------------------------------------*/
/* build option list */
/*---------------------------------------------------------------*/
item_head = item_tail = NULL;
va_start(arg_marker,format_string);
/*---------------------------------------------------------------*/
/* parse the format_string with strtok */
/*---------------------------------------------------------------*/
tok = strtok(format_string," ");
while (NULL != tok)
{
/*------------------------------------------------------------*/
/* allocate area for a new Item */
/*------------------------------------------------------------*/
item = (Cmdline_Item *) malloc(sizeof(Cmdline_Item));
if (NULL == item)
{
puts("Error allocating memory in parsearg()");
return;
}
/*------------------------------------------------------------*/
/* start assigning values to it */
/*------------------------------------------------------------*/
item->next = NULL;
item->sw_char = *tok++;
/*---------------------------------------------------------*/
/* is it a boolean switch? */
/*---------------------------------------------------------*/
if ('\0' == *tok)
item->type = Boolean_Switch;
/*---------------------------------------------------------*/
/* must be a variable switch */
/*---------------------------------------------------------*/
else
item->type = Variable_Switch;
/*------------------------------------------------------------*/
/* now get the variable pointer */
/*------------------------------------------------------------*/
item->variabl